import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { TableLordConf } from "../../common/table/TableLordConf";
import { LeaderAttributeType, LeaderModuleType } from "./LeaderEnum";
import { LeaderNumericMap } from "./attribute/LeaderNumericMap";
import { VM } from "db://oops-framework/libs/model-view/ViewModel";

/** 
 * 主角属性数据
 * 1、主角数值属性
 */
@ecs.register('LeaderModel')
export class LeaderModelComp extends ecs.Comp {

    /** 提供MVVM组件使用的数据 */
    vm: any = {};
    /** 主角服务器唯一编号 */
    uuid: number = -1;
    /** 合成物编号 */
    get id(): number {
        return this.table.id;
    }
    set id(value: number) {
        if (this.table == null)
            this.table = new TableLordConf();
        this.table.init(value);
        for (const key in LeaderAttributeType) {
            if ((this.table as any)[key]) {
                this.attributes.get(key as LeaderAttributeType).setValue(LeaderModuleType.Base, (this.table as any)[key]);
            }
            else {
                this.attributes.get(key as LeaderAttributeType).setValue(LeaderModuleType.Base, 0);
            }
        }
        this.vm["id"] = this.table.id;
        this.vm["cardName"] = this.table.name;
    }
    table: TableLordConf = null!;
    /** 主角数值属性 */
    attributes: LeaderNumericMap = null!;

    /**等级 */
    private _level: number = 1;
    get level(): number {
        return this._level;
    }
    set level(value: number) {
        this._level = value;
        this.vm["level"] = this._level;
    }
    /**进阶：+1 */
    private _stage: number = 0;
    get stage(): number {
        return this._stage;
    }
    set stage(value: number) {
        this._stage = value;
        this.vm["stage"] = this._stage;
    }
    constructor() {
        super();
        this.attributes = new LeaderNumericMap(this.vm);
    }
    vmAdd() {
        this.stage = 0;
        this.level = 1;
        VM.add(this.vm, "Leader");
    }

    vmRemove() {
        VM.remove("Leader");
    }

    reset() {
        this.uuid = -1;
        this._stage = 0;
        this.vmRemove();

        for (var key in this.vm) {
            delete this.vm[key];
        }
    }
}